// pages/buyTicketS/buyTicketS.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        totalmey: 0,
        totalcount: 0,
        isChinese: wx.getStorageSync("isChinese"),
        loadMore: true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this
        that.setData({
            isChinese: wx.getStorageSync("isChinese")
        })
        that.setData({
            eventId: options.eventId,
            id: options.id,
            eventType: options.eventType,
            venueName: app.globalData.venueName,
            eventName: app.globalData.eventName,
            cityName: app.globalData.cityName,
            eventLocation: app.globalData.location
        })
        wx.setNavigationBarTitle({
            title: that.data.isChinese ? "买票" : "Buy Tickets"
        })
        app.getApi.getProducts("POST", {
            eventId: options.eventId,
            method: "opg.eventTicket.find"
        }).then(data => {
            for (let i = 0; i < data.data.result.length; i++) {
                data.data.result[i].count = 0
            }
            that.setData({
                ticket: data.data.result,
                loadMore: false
            })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    jian(e) {
        let index = e.currentTarget.dataset.index,
            price = e.currentTarget.dataset.price,
            ticket = this.data.ticket;

        if (ticket[index].count == 0) {
            return;
        }
        ticket[index].count--
            this.setData({
                ticket: ticket
            })
        this.total(1, price)
    },
    jia(e) {
        let index = e.currentTarget.dataset.index,
            price = e.currentTarget.dataset.price,
            ticket = this.data.ticket;
        if (ticket[index].balance <= 0) {
            wx.showModal({
                title: this.data.isChinese ? "提示" : "Prompt",
                showCancel: false,
                content: this.data.isChinese ? '没有更多票啦!' : 'No more!'
            })
            return;
        }
        if (ticket[index].count >= ticket[index].ticketOneCount && ticket[index].ticketOneCount > 0) {
            // wx.showModal({
            //     title: this.data.isChinese ? "提示" : "Prompt",
            //     showCancel: false,
            //     content: this.data.isChinese ? '单张限购' + ticket[index].ticketOneCount + '张' : 'Limit' + ticket[index].ticketOneCount + 'per order'
            // })
            return;
        }
        ticket[index].count++

            this.setData({
                ticket: ticket
            })
        this.total(0, price)
    },
    total(types, price) {
        let count = this.data.totalcount,
            totalprice = this.data.totalmey;
        if (types == 0) {
            count++
            totalprice = (Number(price) + Number(totalprice)).toFixed(2)
        } else {
            count--
            totalprice = (totalprice - price).toFixed(2)
        }
        this.setData({
            totalcount: count,
            totalmey: totalprice
        })

    },
    pay() {
        if (app.globalData.mobilePhone == "" || app.globalData.mobilePhone == app.globalData.unionId) {
            // wx.showModal({
            //     title: this.data.isChinese ? "提示" : "Prompt",
            //     content: this.data.isChinese ? "该用户并未注册" : "This user didn't register",
            //     showCancel: false,
            //     success: function(res) {
            //         if (res.confirm) {
            wx.navigateTo({
                    url: '../login/login'
                })
                //         } else if (res.cancel) {
                //             console.log("用户点击取消");
                //         }
                //     }
                // });
            return;
        }
        let newdata = this.data.ticket,
            newarr = [];
        for (let i = 0; i < newdata.length; i++) {
            if (newdata[i].count > 0) {
                newarr.push(newdata[i])
            }
        }
        if (newarr.length > 0) {
            wx.navigateTo({
                url: '../pay/pay?eventId=' + this.data.eventId + "&id=" + this.data.id + "&eventType=" + this.data.eventType + "&venueName=" + this.data.venueName + "&eventName=" + this.data.eventName + "&cityName=" + this.data.cityName + "&isType=0",
                success: function(res) {},
                fail: function(res) {},
                complete: function(res) {},
            })
            wx.setStorageSync('payarr', newarr)
        } else {
            wx.showModal({
                title: this.data.isChinese ? "提示" : "Prompt",
                showCancel: false,
                content: this.data.isChinese ? '请至少选择1张门票' : 'Select 1 ticket at least'
            })
            return;

        }
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})